package cn.yunhe.dao.impl;

import cn.yunhe.dao.StreetDao;
import cn.yunhe.model.Street;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/*spring注解*/
@Repository(value = "streetDao")
public class StreetDaoImpl extends HibernateDaoSupport implements StreetDao {

    @Autowired
    @Qualifier("sessionFactory")
    public void setSessionFactory111(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }


    @Override
    public boolean insertStreet(Street street) {
        getHibernateTemplate().save(street);
        return false;
    }

    @Override
    public boolean updateStreet(Street street) {
        getHibernateTemplate().update(street);
        return false;
    }

    @Override
    public boolean deleteStreet(Integer sid) {
        getHibernateTemplate().delete(this.getById(sid));
        return false;
    }

    @Override
    public Street getById(Integer sid) {
        Street street=getHibernateTemplate().get(Street.class,sid);
        return null;
    }

    @Override
    public List<Street> searchStreet(String hql) {
        List<Street>streetList=getHibernateTemplate().find(hql);
        return streetList;
    }

    @Override
    public List<Street> searchStreet(final Street street) {
        List<Street>streetList=getHibernateTemplate().executeFind(new HibernateCallback<List<Street>>() {
            @Override
            public List<Street> doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria=session.createCriteria(Street.class);

                if (street!=null){
                    if (street.getSid()!=null&&street.getSid()>0){
                        criteria.add(Restrictions.eq("sid",street.getSid()));
                    }
                    if (street.getSname()!=null&&street.getSname().trim().length()>0){
                        criteria.add(Restrictions.like("sname",street.getSname(),MatchMode.ANYWHERE));
                    }
                    if (street.getDistrict()!=null&&street.getDistrict().getDid()!=null&&street.getDistrict().getDid()>0){
                        Criteria criteria2District= criteria.createCriteria("district");
                        criteria2District.add(Restrictions.eq("did",street.getDistrict().getDid()));
                    }
                }

                List<Street>streetList1=criteria.list();
                return streetList1;
            }
        });
        return null;
    }
}
